package com.roc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.bean.orgMainBean;
import com.roc.dao.DaoException;
import com.roc.dao.IorgDao;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.TblArea;
import com.roc.pojo.TblInsInf;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

@Repository
public class OrgImpl extends BaseDaoImpl implements IorgDao {
	
	@Override
	public List<TblInsInf> queryAllOrgList() throws DaoException {
		List<TblInsInf> orgList=new ArrayList<TblInsInf>();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TblInsInf o where InsCompanyCd= '85270000' and InsType='1' ";
			Query query=session.createQuery(hql);
			orgList=query.list();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return orgList;
	}

	@Override
	public List<TblInsInf> queryOrgList() throws DaoException {
		List<TblInsInf> orgList=new ArrayList<TblInsInf>();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TblInsInf o where InsCompanyCd= '85270000' and InsType='1' ";
			Query query=session.createQuery(hql);
			orgList=query.list();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return orgList;
	}

	@Override
	public OrgBean queryOrgByCode(String org_code) throws DaoException {
		OrgBean ob=null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from OrgBean o where o.org_code= :org_code ";
			Query query=session.createQuery(hql);
			query.setString("org_code", org_code);
			ob=(OrgBean)query.uniqueResult();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return ob;
	}

	
	@SuppressWarnings("rawtypes")
	@Override
	public List lisOrgByMerchantId(long merchantId) throws DaoException {
		List<OrgBean> orgBeanList=new ArrayList<OrgBean>();
		try{
			Session session=sessionFactory.getCurrentSession();
			String sql=" SELECT o.org_code,o.org_name FROM t_org o,t_merchant_org mo WHERE "+ 
                       " o.id=mo.org_id AND mo.merchant_id=? order by o.dt_create ";
			Query query=session.createSQLQuery(sql);
			List<Object> params=new ArrayList<Object>();
			params.add(merchantId);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			
			List searchRsList=query.list();
			if(searchRsList!=null&&searchRsList.size()>0){
				for(int i=0;i<searchRsList.size();i++){
					Object[] obj=(Object[])searchRsList.get(i);
					OrgBean bean=new OrgBean();
					bean.setOrg_code((String)obj[0]);
					bean.setOrg_name((String)obj[1]);
					orgBeanList.add(bean);
				}
			}
		}catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("根据操作员账号查询机构信息出错");
		}
		return orgBeanList;
	}

	@Override
	public List<OrgBean> listOrgs(Operators operators) throws DaoException {
		// TODO Auto-generated method stub
		String hql=" from OrgBean o where  1=1 ";
		List<Object> params=new ArrayList<Object>();
		List<Object> tepList=new ArrayList<Object>();
		List<OrgBean> orgList=new ArrayList<OrgBean>();
		
		try {
			if(operators==null||operators.getRoleType()==null||operators.getFlag()==null){
				throw new DaoException("查询机构信息出错");
			}
			if(Constance.OPERATORS_FLAG_OEM.equals(operators.getFlag())){
				hql+=" and o.org_code=?";
				params.add(operators.getOrgCode());
			}else{
				if(Role.AGENT.getCode().equals(operators.getRoleType())||
						Role.MERCHANT.getCode().equals(operators.getRoleType())||Role.AGENT_BUSS.getCode().equals(operators.getRoleType())){
					hql+=" and o.org_code=?";
					params.add(operators.getOrgCode());
				}
			}
			
			Session session = sessionFactory.getCurrentSession();
			
			Query query=session.createQuery(hql);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++){
					query.setParameter(i, params.get(i));
				}
				
			}
			tepList=query.list();
			if(tepList!=null&&tepList.size()>0){
				for(Object obj:tepList){
					OrgBean bean=(OrgBean)obj;
					orgList.add(bean);
				}
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return orgList;
	}
	
	@Override
	public List<TblInsInf> queryOrg(TblInsInf inf) throws DaoException {
		String hql=" from TblInsInf o where InsCompanyCd= '85270000' and InsType='1'";
		List<TblInsInf> orgList=new ArrayList<TblInsInf>();
		
		try {
			Session session = sessionFactory.getCurrentSession();
			
			Query query=session.createQuery(hql);
		
			orgList=query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return orgList;
	}

	@Override
	public List<TblArea> listAreaByOrgCode(String orgCode) throws DaoException {
		List<TblArea> aredList=new ArrayList<TblArea>();
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TblArea where insCode= "+orgCode+" and status=1";
			Query query=session.createQuery(hql);
			aredList=query.list();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询区域信息出错");
		}
		return aredList;
	}

	@Override
	public TblInsInf queryOrgByOrgCode(String org_code) throws DaoException {
		TblInsInf ob=null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TblInsInf o where o.InsIdCd= :org_code ";
			Query query=session.createQuery(hql);
			query.setString("org_code", org_code);
			ob=(TblInsInf)query.uniqueResult();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return ob;
	}

	@Override
	public TblInsInf queryInsByCode(String code) throws DaoException {
		TblInsInf ob=null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TblInsInf o where o.InsIdCd= :org_code ";
			Query query=session.createQuery(hql);
			query.setString("org_code", code);
			ob=(TblInsInf)query.uniqueResult();
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询机构信息出错");
		}
		return ob;
	}

	@Override
	public List<orgMainBean> listOrgMainByOrgCode(String orgCode) throws DaoException {
		List<orgMainBean> orgMainList=new ArrayList<orgMainBean>();
		try {

			Session session=sessionFactory.getCurrentSession();
			String sql=" select * from  TBL_ORG_MAIN t where t.super_id = '" + orgCode + "' ";
			Query query=session.createSQLQuery(sql);
		/*	List<Object> params=new ArrayList<Object>();
			params.add(merchantId);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}*/
			
			List searchRsList=query.list();
			if(searchRsList!=null&&searchRsList.size()>0){
				for(int i=0;i<searchRsList.size();i++){
					Object[] obj=(Object[])searchRsList.get(i);
					orgMainBean bean=new orgMainBean();
					bean.setOrgCode( (String)obj[0] );
					bean.setOrgName( (String)obj[1] );
					orgMainList.add(bean);
				}
			}
			
			/*
			Session session = sessionFactory.getCurrentSession();
			String hql="from orgMainBean where superId =  " + orgCode;
			Query query=session.createQuery(hql);
			orgMainList=query.list();*/
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询小机构信息出错");
		}
		return orgMainList;
	}


	@Override
	public orgMainBean queryOrgMainByOrgMain(String orgMain) throws DaoException {
		orgMainBean bean = new orgMainBean();
		try {

			Session session=sessionFactory.getCurrentSession();
			String sql=" select * from  TBL_ORG_MAIN t where t.org_code = '" + orgMain + "' ";
			Query query=session.createSQLQuery(sql);
			
			Object ob = query.uniqueResult();
			if( null != ob ) {
				
				Object[] obj = (Object[])ob;
				bean.setOrgCode( (String)obj[0] );
				bean.setOrgName( (String)obj[1] );
				bean.setAreaId( (String)obj[2] );
			}
			
		} catch (HibernateException e) {
			logger.debug(e.getMessage(), e);
			throw new DaoException("查询某个机构管理信息出错");
		}
		return bean;
	}
}
